math.randomseed(os.time())
backfinal = Image.load("Sprites/backfinal.png")
bullet = Image.load("Sprites/chakra.png")
Resolution = { width = 480, height = 272 }
red = Color.new(255,0,0)
white = Color.new(255,255,255)

lefttable = { 
Image.load("./Sprites/Naruto/esquerda1.png"),
Image.load("./Sprites/Naruto/esquerda2.png"),
Image.load("./Sprites/Naruto/esquerda3.png"),
Image.load("./Sprites/Naruto/esquerda4.png"),
Image.load("./Sprites/Naruto/esquerda5.png"),
Image.load("./Sprites/Naruto/esquerda6.png"),
}
righttable = { 
Image.load("./Sprites/Naruto/direita1.png"),
Image.load("./Sprites/Naruto/direita2.png"),
Image.load("./Sprites/Naruto/direita3.png"),
Image.load("./Sprites/Naruto/direita4.png"),
Image.load("./Sprites/Naruto/direita5.png"),
Image.load("./Sprites/Naruto/direita6.png"),
}
standtable = { 
Image.load("./Sprites/Naruto/st1.png"),
Image.load("./Sprites/Naruto/st2.png"),
Image.load("./Sprites/Naruto/st3.png"),
Image.load("./Sprites/Naruto/st4.png"),
Image.load("./Sprites/Naruto/st5.png"),
Image.load("./Sprites/Naruto/st6.png"),
}
s = Image.load("./Sprites/Naruto/st1.png")
jump1 = Image.load ("./Sprites/Naruto/salto1.png")
jump2 = Image.load ("./Sprites/Naruto/salto2.png")
ss = Image.load("./Sprites/Enimigos/enimigo.png")

Enemylefttable = { 
Image.load("./Sprites/Sasuke/esquerda1.png"),
Image.load("./Sprites/Sasuke/esquerda2.png"),
Image.load("./Sprites/Sasuke/esquerda3.png"),
Image.load("./Sprites/Sasuke/esquerda4.png"),
Image.load("./Sprites/Sasuke/esquerda5.png"),
Image.load("./Sprites/Sasuke/esquerda6.png"),
}
Enemyrighttable = { 
Image.load("./Sprites/Sasuke/direita1.png"),
Image.load("./Sprites/Sasuke/direita2.png"),
Image.load("./Sprites/Sasuke/direita3.png"),
Image.load("./Sprites/Sasuke/direita4.png"),
Image.load("./Sprites/Sasuke/direita5.png"),
Image.load("./Sprites/Sasuke/direita6.png"),
}
Enemystandtable = { 
Image.load("./Sprites/Sasuke/st1.png"),
Image.load("./Sprites/Sasuke/st2.png"),
Image.load("./Sprites/Sasuke/st3.png"),
Image.load("./Sprites/Sasuke/st4.png"),
Image.load("./Sprites/Sasuke/st5.png"),
Image.load("./Sprites/Sasuke/st6.png"),
}
ss = Image.load("./Sprites/Sasuke/st1.png")
jump11 = Image.load("./Sprites/Sasuke/jump1.png")
jump22 = Image.load("./Sprites/Sasuke/jump2.png")

--Tabela do jogador
sd = { st1,st2,st3, st4, st5, st6 }
l = { l1,l2,l3,l4,l5,l6 }
r = { r1,r2,r3,r4,r5,r6 }
Player = { x = 30, y = 210, img = s, d = u, direction = "right", gy = 210, gravidade = 201, jumpstate = "ground", jumpspeed = 10, life = 100  }

--Tabela inimigo final
se = { stt1,stt2,stt3, stt4,stt5,stt6 }
esq = { e1,e2,e3,e4, e5, e6 }
dir = { d1,d2,d3,d4, d5, d6 }
Enemyfinal = { x= 350, y = 202, dd = uu, imag = ss, direction = "right", gravidade = 203, jumpspeed = 10, jumpstate = "ground", ecra = "true", life = 100 }

--Tabela da bala jogador
BulletInfo = {}
for a = 1,10 do
BulletInfo[a] = { pic = bullet , firing = false, damage = 1, direction = "right", x = Player.x , y = Player.y }
end

--Tabela da bala inimigo final
BulletInfo2 = {}
for v = 1,10 do
BulletInfo2[v] = { pic = bullet , firing = false, damage = 1, direction = "right", x = Enemyfinal.x , y = Enemyfinal.y }
end

--Tabela energia inimigo final
healthbar = {}
healthbar.y = 10 

--Variaveis
oldpad = Controls.read()
currentBullet = 0
direction = "left"
animtimer = 0

--Funcoes

function bulletSetup()
dofile("./Script/FinalStage/bulletsetup.txt")
end

function bulletSetup2()
dofile("./Script/FinalStage/bulletsetup2.txt")
end

function playerFire()
dofile("./Script/FinalStage/playerfire.txt")
end

function playerjump()
if Player.jumpstate == "jumping" then
Player.jumpspeed = Player.jumpspeed - 0.5
Player.gravidade = Player.gravidade - Player.jumpspeed
Player.img = jump
end

if Player.gravidade < 0 then
Player.jumpstate = "falling"
end

if Player.gravidade < 200 and Player.jumpstate == "falling" then
Player.gravidade = Player.gravidade + (Player.jumpspeed + 3)
end

if Player.gravidade == 200 then
Player.jumpspeed = 10
Player.jumpstate = "ground"
end

if Player.gravidade > 200
then Player.gravidade = 200
end

Player.y = Player.gravidade
end

function Enemy()
dofile("./Script/FinalStage/Enemy.txt")
end

function enemyfinalFire()
dofile("./Script/FinalStage/enemyfinalfire.txt")
end

function enemyfinalhit(BulletInfo, Enemyone)
for h = 1,10 do
if BulletInfo[h].firing then
if (BulletInfo[h].x + bullet:width() > Enemyone.x) and (BulletInfo[h].x < Enemyone.x + ss:width()) and (BulletInfo[h].y + bullet:height() > Enemyone.y) and (BulletInfo[h].y < Enemyone.y + ss:height())  then
BulletInfo[h].firing = false
Enemyone.life = Enemyone.life - 1
end
end
if Enemyone.life == 0 then
System.memclean()
dofile("./Script/Creditos.txt")
end
end
end

function playerhit(BulletInfo2, Enemyone)
for h = 1,10 do
if BulletInfo2[h].firing then
if (BulletInfo2[h].x + bullet:width() > Enemyone.x) and (BulletInfo2[h].x < Enemyone.x + ss:width()) and (BulletInfo2[h].y + bullet:height() > Enemyone.y) and (BulletInfo2[h].y < Enemyone.y + ss:height())  then
BulletInfo2[h].firing = false
Player.life = Player.life - 0.5
end
end
if Player.life == 0 then
System.memclean()
dofile("./Script/Creditos.txt")
end
end
end

function animate(imgtable,interval) 
local numFrames = table.getn(imgtable) 
if animtimer >= numFrames*interval then animtimer = 0 end 
animtimer = animtimer + 1 
for i = 1, numFrames do 
if animtimer > interval*(i-1) and animtimer <= interval*i then 
img = imgtable[i] 
break 
end
end
return img 
end

--Loop principal
while true do

screen:clear()
pad = Controls.read()
Enemyfinal.AI = math.random(1, 40)

screen:blit(0,0, backfinal,0, 0, backfinal:width(), backfinal:height(), false)

if pad:square() and oldpad:square() ~= pad:square() then
bulletSetup()
end

if pad:cross() and not oldpad:cross() and Player.jumpstate == "ground" then
Player.jumpstate = "jumping"
end
playerjump()

if pad:left() and Player.x > 0 and Player.jumpstate == "ground" then
Player.x = Player.x - 1.5
Player.img = animate(lefttable,10)
direction="left"
else
Player.img = animate(standtable,15)
end
if pad:right() and Player.x < 500-Player.img:width() and Player.jumpstate == "ground" then
Player.x = Player.x + 1.5
Player.img = animate(righttable,15)
direction="right"
end
if pad:down() then
direction = "down"
end
if pad:up() then
direction = "up"
end

if Player.jumpstate == "jumping" and direction == "left" then 
Player.x = Player.x - 1.5
Player.img = jump2
end
if Player.jumpstate == "jumping" and direction == "right" then
Player.x = Player.x + 1.5
Player.img = jump1
end
if Player.jumpstate == "jumping" and direction == "up" then
Player.x = Player.x + 1.5
Player.img = jump1
end

if Enemyfinal.jumpstate == "jumping" then
Enemyfinal.jumpspeed = Enemyfinal.jumpspeed - 0.5
Enemyfinal.gravidade = Enemyfinal.gravidade - Enemyfinal.jumpspeed
Enemyfinal.imag = jump22
end

if Enemyfinal.gravidade < 0 then
Enemyfinal.jumpstate = "falling"
end

if Enemyfinal.gravidade < 202 and Enemyfinal.jumpstate == "falling" then
Enemyfinal.gravidade = Enemyfinal.gravidade + (Enemyfinal.jumpspeed + 3)
end

if Enemyfinal.gravidade == 202 then
Enemyfinal.jumpspeed = 10
Enemyfinal.jumpstate = "ground"
end

if Enemyfinal.gravidade > 202
then Enemyfinal.gravidade = 202
end

Enemyfinal.y = Enemyfinal.gravidade

if Enemyfinal.jumpstate == "ground" then
Enemyfinal.imag = ss
end

if Enemyfinal.x >Player.x  + Player.img:width() and Enemyfinal.jumpstate == "ground" then
Enemyfinal.x = Enemyfinal.x - 1
Enemyfinal.imag = animate(Enemylefttable,15)
Enemyfinal.direction = "left"
else
Enemyfinal.imag = animate(Enemystandtable,15)
end
if Enemyfinal.x < Player.x - Player.img:width() and Enemyfinal.jumpstate == "ground" then
Enemyfinal.x = Enemyfinal.x + 1
Enemyfinal.imag = animate(Enemyrighttable,15)
Enemyfinal.direction = "right"
end

--Attack--

if Enemyfinal.AI == 10 or Enemyfinal.AI == 20 then
bulletSetup2()
end
if Enemyfinal.AI == 10 or Enemyfinal.AI == 20 then
bulletSetup2()
end

--Defense--

if Enemyfinal.x == Player.x - Player.img:width() then
Enemyfinal.jumpstate = "jumping"
Enemyfinal.imag = jump22
end

if Enemyfinal.x == Player.x + Player.img:width() then
Enemyfinal.jumpstate = "jumping"
Enemyfinal.imag = jump22
end

playerFire()
enemyfinalFire()
enemyfinalhit(BulletInfo,Enemyfinal)
playerhit(BulletInfo2, Player)

screen:fillRect(10,10,Enemyfinal.life,healthbar.y,red) 
screen:print(10,30,Enemyfinal.life,white) 
screen:fillRect(170,10,Player.life,healthbar.y,red) 
screen:print(170,30,Player.life,white) 
screen:blit(Player.x,Player.y,Player.img)
screen:blit(Enemyfinal.x,Enemyfinal.y,Enemyfinal.imag)
screen.waitVblankStart()
screen.flip()
oldpad = pad
end 